<?php
// ------------------------------------------------------------------------- 
//	program_schedule
//		Copyright 2005, PichardDesign
// 		http://www.picharddesign.com
// ------------------------------------------------------------------------- 
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  You may not change or alter any portion of this comment or credits       //
//  of supporting developers from this source code or any supporting         //
//  source code which is considered copyrighted (c) material of the          //
//  original comment or credit authors.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
//  ------------------------------------------------------------------------ //
require_once "../../mainfile.php";
//include XOOPS_ROOT_PATH.'/header.php';
echo "<LINK href=\"program_schedule.css\" rel=\"stylesheet\" type=\"text/css\">\n";




//#########################################################
//#########################################################
//#########################################################


	$weekViewData = array();
	$result = $xoopsDB->query( "SELECT * FROM ".$xoopsDB->prefix("program_schedule_programs")." ORDER BY start_time asc, mon DESC, tue DESC, wed DESC, thu DESC, fri DESC, sat DESC, sun DESC" ) ;
		if ($result){
			$x=0;
			while ($myrow = $xoopsDB->fetchArray($result)){
				$weekViewData[] = $myrow;
			++$x;
			}
		}
		


 	
	echo("<TABLE width=\"100%\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\">");
	echo("<TR><TD valign=\"top\" align=\"left\"><table width='100%'><tr> <td align='center'><span style='color:#666666; padding-bottom:3px; padding-top:3px; font-weight:600; font-size:18px; text-decoration:underline;'>".$_SESSION['letters']." Program Guide</span></td> </tr></table></TD></TR>");
	//Conjur Weekly View
	echo "<tr><td><table width='100%' cellspacing='0' cellpadding='0'>\n";
	//Column Headers
	echo "<tr class='columnHeaderYo'><td width='9.7%'>&nbsp;</td><td width='12.9%'>Monday</td><td width='12.9%'>Tuesday</td><td width='12.9%'>Wednesday</td><td width='12.9%'>Thursday</td><td width='12.9%'>Friday</td><td width='12.9%'>Saturday</td><td width='12.9%'>Sunday</td>	</tr>\n\n";

		//figure out columns and rows
		$arrayPointer = 0;
		function getRows($loopCounter,$minutes){
		global $arrayPointer;
		global $weekViewData;
		$days = array('mon','tue','wed','thu','fri','sat','sun');
		
		//find out start time of program and loop row
		$advancePointer = "";
		$currenrRowData = array();//temp array for all entries in this row
		for ($i = $arrayPointer; $i < count($weekViewData); $i++) {
			if(date("h:i a",$weekViewData[$arrayPointer]['start_time']) == date("h:i a",mktime($loopCounter,$minutes,00,09,02,1973))){
				$currenrRowData[] = $weekViewData[$arrayPointer];//get all progam entries for curent row
				++$arrayPointer;
			}else{
				break;
			}
		}
		
		
		//Figure out number of dyas/cells for each entry
		$rowString = "";
		for ($i = 0; $i < count($currenrRowData); $i++) {
		$activeDays = array();
		$non_activeDays = array();
		
			for ($r = 0; $r < (count($days)+1); $r++) {
			
				if(isset($currenrRowData[$i][$days[$r]]) && $currenrRowData[$i][$days[$r]] == 'true'){
					$activeDays[] = $days[$r];//number of days prgram has
				}else{//build cell
					$rowSpan = ($currenrRowData[$i]['end_time'] - $currenrRowData[$i]['start_time'])/1800;
					if(count($activeDays) > '0' && $currenrRowData[$i]['link'] != ""){
						$rowString = $rowString . "<td class='weekDisplay'  colspan='".count($activeDays)."' rowspan='$rowSpan'>".$currenrRowData[$i]['title'] ."</td>";
					}elseif(count($activeDays) > '0' && $currenrRowData[$i]['link'] == ""){
						$rowString = $rowString . "<td class='weekDisplay'  colspan='".count($activeDays)."' rowspan='$rowSpan'>".$currenrRowData[$i]['title'] ."</td>";
					}
				$activeDays = array();//reset array
				}
			}//end days loop
			
			
		}//end loop


	echo $rowString;
		
	}//end function
	
	
	

	//Loop through and build the rows...
	for ($i = 0; $i < 24; $i++) {
	
			//if(mktime($i,00,00,09,02,1973) >= $_SESSION['start_time']){
			echo"<tr><td class='timeColumn' nowrap>".date("h:i a",mktime($i,00,00,09,02,1973))."</td>";
				getRows($i,00);
			echo"</tr>\n";
			//}
			
			//for 30 minute block
			//if(mktime($i,30,00,09,02,1973) >= $_SESSION['start_time']){
			echo"<tr><td class='timeColumn' nowrap>".date("h:i a",mktime($i,30,00,09,02,1973))."</td>";
				getRows($i,30);
			echo"</tr>\n";
			//}
	}
	echo"</table></td></tr>";


	
	echo("</TABLE>");


?>